package tests;

import java.io.*;

public class CompilerTest extends CommonTest {
    private final String COMP_STRING = "java -cp ./antlr-4.0-complete.jar:asm-all-4.1.jar:./ Main -p src -n Src";
    private final String RUN_STRING = "java SrcRunner";

    compiler.Compiler c = new compiler.Compiler();

    @Override
    protected String run(String q) {


        File s = new File("src");
        try {
            PrintWriter src = new PrintWriter(s);
            src.println(q);
            src.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        try {
            Process c = Runtime.getRuntime().exec(COMP_STRING);
            BufferedReader cInput = new BufferedReader(new InputStreamReader(c.getInputStream()));
            String b;
            while ((b = cInput.readLine()) != null) {
                System.out.println(b);
            }
            Process p = Runtime.getRuntime().exec(RUN_STRING);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            return stdInput.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }


}
